import axios, { AxiosRequestConfig } from "axios";
axios.defaults.baseURL = "https://m.maizuo.com";
import { Toast } from 'vant';
import 'vant/es/toast/style';
axios.interceptors.request.use(
    // 这里是引入了 axios 的一个配置约束 interface ，然后我们这个参数，进行规范性约束
    (config: AxiosRequestConfig): AxiosRequestConfig => {
        let host = "mall.film-ticket.film.list";
        // 根据不同的路由状态设置 info
        const info = config.headers && config.headers.info;
        if (info === "user") {
            return config;
        }
        if (info === "cinema") {
            host = "mall.film-ticket.cinema.list";
        }
        if (info === "info") {
            host = "mall.film-ticket.film.info";
        }
        if (info === "city") {
            host = "mall.film-ticket.city.list";
        }
        if (info === "news") {
            host = "mall.act.static-page.info";
        }
        // .....后续如果有其他的数据请求需求，就接着写
        config.headers = {
            "X-Client-Info":
                '{"a":"3000","ch":"1002","v":"5.0.4","e":"16006566672048699400193","bc":"110100"}',
            "X-Host": host,
        };
        return config;
    },
    error => {
        Toast("网络请求错误，请检查网络！")
        return Promise.reject(error);
    }
)
axios.interceptors.response.use(res => {
    // 这些数据的返回，都是我们的后端跟我们的约束
    if (res.data.msg === "ok" || res.data.info === "success" || res.data.info === "OK") {
        Toast("成功返回数据！");
        return res.data;
    } else {
        Toast("数据错误反馈，请检查输入！")
    }
}, err => {
    console.log(err);
    Toast("网络请求错误，请检查网络！")
});
export default axios;